package Action.Manager;

import Action.Sys.Setting;
import Bean.BookInfoEntity;
import Bean.Log.HistoricalData;
import Bean.Log.SystemLogEntity;
import Bean.Log.UserLogEntity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import tools.HibernateTools;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Blob;

public class AddBookInfo extends ActionSupport {
    private File book_picture_File;//上传文件的file对象，由拦截器自动填充
    private String book_picture_FileFileName;//上传文件名，由拦截器自动填充
    private String book_picture_FileContentType;//上传文件类型，由拦截器自动填充

    int id;
    String book_name;
    String book_Author;
    String book_Info;
    String book_tag1;
    String book_tag2;
    String book_tags;
    Blob book_picture;
    int  vister;
    int sum;
    int used;
    HttpServletRequest request;
    String err="";
    @Override
    public String execute() throws Exception {
        clearErrorsAndMessages();
        request = ServletActionContext.getRequest();
        //检查管理员权限--可做二次权限校验
        if (!checkManage()) {
            addActionError("权限不足,请您先登录");
            return INPUT;
        }
        if (book_name==null)
            return INPUT;//首次打开
        //数据合法性检查
        if (!UserDataCheck()){
            addActionError(err);
            return INPUT;
        }
        //数据封装
        BookInfoEntity bookInfoEntity=getBookinfoEntity();
        //修改数据库
        Session session = HibernateTools.getSession();
        session.save(bookInfoEntity);


        HistoricalData historicalData=session.get(HistoricalData.class, tools.TimeTools.formatTime(0));
        if (historicalData==null){
            historicalData=new HistoricalData();
            historicalData.setTimestamp(tools.TimeTools.formatTime(0));
        }
        historicalData.setNewBookNum(historicalData.getNewBookNum()+1);
        session.saveOrUpdate(historicalData);

        addActionMessage("图书添加成功");
        SystemLogEntity.addLog((Integer) ActionContext.getContext().getSession().get("adminid"), this, Setting.SystemLog_flag_AddBook,bookInfoEntity.getId(),SystemLogEntity.remark_book);
        HibernateTools.commit(session);
        return SUCCESS;
    }



    private boolean UserDataCheck() {
        //数据完整度检测
        if (book_name==null||book_name.equals(""))
            err+="请输入书名\n";
        if (book_Author==null||book_Author.equals(""))
            err+="请输入作者\n";
        if (book_tag1==null||book_tag1.equals(""))
            err+="请至少填写一个标签\n";
        if (book_Info==null||book_Info.equals(""))
            err+="请输入完善图书简介\n";
        if (err.length()>0)
            return false;
        //用户合法性检测
        Session session=HibernateTools.getSession();
        if (session.createQuery("from BookInfoEntity book where book_name= ?").setParameter(0, book_name).list().size()!=0) {
            err+="图书信息重复";
            HibernateTools.commit(session);
            return false;
        }
        return true;
    }

    private boolean checkManage() {

        return true;
    }

    public BookInfoEntity getBookinfoEntity() {
        BookInfoEntity bookInfoEntity=new BookInfoEntity();
        bookInfoEntity.setBook_name(book_name);
        bookInfoEntity.setBook_Author(book_Author);
        bookInfoEntity.setBook_Info(book_Info);
        bookInfoEntity.setBook_tag1(book_tag1);
        bookInfoEntity.setBook_tag2(book_tag2==null?"":book_tag2);
        bookInfoEntity.setSum(sum);
        bookInfoEntity.setVister(vister);
        bookInfoEntity.setUsed(used);

        bookInfoEntity.setBook_picture(HibernateTools.File_To_Blob(book_picture_File));
        return bookInfoEntity;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBook_name() {
        return book_name;
    }

    public void setBook_name(String book_name) {
        this.book_name = book_name;
    }

    public String getBook_Author() {
        return book_Author;
    }

    public void setBook_Author(String book_Author) {
        this.book_Author = book_Author;
    }

    public String getBook_Info() {
        return book_Info;
    }

    public void setBook_Info(String book_Info) {
        this.book_Info = book_Info;
    }

    public String getBook_tag1() {
        return book_tag1;
    }

    public void setBook_tag1(String book_tag1) {
        this.book_tag1 = book_tag1;
    }

    public String getBook_tag2() {
        return book_tag2;
    }

    public void setBook_tag2(String book_tag2) {
        this.book_tag2 = book_tag2;
    }

    public String getBook_tags() {
        return book_tags;
    }

    public void setBook_tags(String book_tags) {
        this.book_tags = book_tags;
    }

    public Blob getBook_picture() {
        return book_picture;
    }

    public void setBook_picture(Blob book_picture) {
        this.book_picture = book_picture;
    }

    public int getVister() {
        return vister;
    }

    public void setVister(int vister) {
        this.vister = vister;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    public int getUsed() {
        return used;
    }

    public void setUsed(int used) {
        this.used = used;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String getErr() {
        return err;
    }

    public void setErr(String err) {
        this.err = err;
    }

    public File getBook_picture_File() {
        return book_picture_File;
    }

    public void setBook_picture_File(File book_picture_File) {
        this.book_picture_File = book_picture_File;
    }

    public String getBook_picture_FileFileName() {
        return book_picture_FileFileName;
    }

    public void setBook_picture_FileFileName(String book_picture_FileFileName) {
        this.book_picture_FileFileName = book_picture_FileFileName;
    }

    public String getBook_picture_FileContentType() {
        return book_picture_FileContentType;
    }

    public void setBook_picture_FileContentType(String book_picture_FileContentType) {
        this.book_picture_FileContentType = book_picture_FileContentType;
    }
}
